// miniprogram/rember1/r1-brandDetail/brandDetail.js
let api = require('../../utils/api.js')
let myrequest = require('../../utils/myRequest.js')
let bindnav = require('../../utils/path.js') //引入路径缩写

Page({

  /**
   * 页面的初始数据
   */
  data: {
    newflag:true
  },
  // 引入path的bindnav
  bindnav: bindnav,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { id } = options;
    // 通过brand的id 获取该品牌的商品列表
    myrequest.myRequest({
      url: api.detail,
      data: {
        id
      },
      success: (res) => {
        // 获取各项数据并填入data
        console.log('brandlist',res)
        let data = res.data.data;
        let brand = data.brand;
        let { name, list_pic_url, id, simple_desc } = brand;
        console.log("simple_desc", simple_desc);

        let teststr = simple_desc.split("，\n");
        console.log(teststr)
        simple_desc = teststr.toString();
        console.log(simple_desc)

        this.setData({
          id, //将该品牌id号存进data中以便底部调用api
          name , list_pic_url,  simple_desc
        })
      }
    })

    myrequest.myRequest({
      url: api.list,
      data: {
        id
      },
      success:(res)=>{
        console.log('list',res);
        // let goodslist = res.data.data.data
        let { pageSize, currentPage, totalPages, data: goodslist } = res.data.data
        console.log("goodslist",goodslist);
        this.setData({
          goodslist,  //列表子项
          currentPage,  //当前页
          pageSize, //单页展示子项数
          totalPages //以当前展现数预算出总可显示页数
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let { id, currentPage, pageSize, totalPages, newflag} = this.data;
    console.log(id, currentPage, pageSize, totalPages, newflag);
    if (currentPage == totalPages || newflag == false) {
      return
    }
    this.setData({
      newflag:false
    })
    myrequest.myRequest({
      url: api.list,
      data: {
        id,
        page: ++currentPage,
        size: pageSize
      },
      success: (res) => {
        console.log('list', res);
        // let goodslist = res.data.data.data
        let { goodslist } = this.data;
        let { pageSize, currentPage, totalPages, data: newgoodslist } = res.data.data
        console.log("newres", res);
          this.setData({
            newflag: true,
            goodslist: goodslist.concat(newgoodslist),  //列表子项
            currentPage,  //当前页
            pageSize, //单页展示子项数
            totalPages //以当前展现数预算出总可显示页数
          })
      }
    })
    
    // this.setData({
    //   currentPage
    // })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})